import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors = "Aut0r", keywords = "Path Get", name = "Aut0PathRecorder", version = 0.0001, description = "Records tile data to text to document.")
public class Aut0PathRecorder extends Script implements PaintListener {

	ArrayList<RSTile> t = new ArrayList<RSTile>();
	boolean guiWait = true;
	String name = "Tiles.txt";

	public boolean onStart() {
		String r = JOptionPane.showInputDialog(null,
			"What to name the log file?",
			"Aut0r Path Maker Settings",
			JOptionPane.QUESTION_MESSAGE);
		name = r + ".txt";
		guiWait = false;
		while (guiWait)
			sleep(100);
		env.setUserInput(env.INPUT_MOUSE
				| env.INPUT_KEYBOARD);
		return true;
	}

	@Override
	public int loop() {
		if (!t.contains(players.getMyPlayer().getLocation()))
			t.add(players.getMyPlayer().getLocation());
		return 100;
	}

	public void onFinish() {
		FileWriter fWriter = null;
		BufferedWriter writer = null;
		try {
			File file = new File(name);
			fWriter = new FileWriter(file);
			writer = new BufferedWriter(fWriter);
			writer.write("RSTile[] = { ");
			for (int i = 0; i < (t.size() - 1); i++) {
				writer.write("new RSTile(" + t.get(i).getX() + ", "
						+ t.get(i).getY() + "), ");
			}
			writer.write("new RSTile(" + t.get(t.size() - 1).getX() + ", "
					+ t.get(t.size() - 1).getY() + ")");
			writer.write(" };");
			writer.close();
		} catch (Exception e) {

		}
	}

	private void drawTile(Graphics g, RSTile tile, Color color) {
		if (tile == null)
			return;
		if (!calc.tileOnScreen(tile))
			return;
		Point south_west = calc.tileToScreen(tile, 0, 0, 0);
		Point south_east = calc.tileToScreen(new RSTile(tile.getX() + 1,
				tile.getY()), 0, 0, 0);
		Point north_west = calc.tileToScreen(
				new RSTile(tile.getX(), tile.getY() + 1), 0, 0, 0);
		Point north_east = calc.tileToScreen(new RSTile(tile.getX() + 1,
				tile.getY() + 1), 0, 0, 0);

		if (calc.pointOnScreen(south_west)
				&& calc.pointOnScreen(south_east)
				&& calc.pointOnScreen(north_east)
				&& calc.pointOnScreen(north_west)) {
			g.setColor(Color.BLACK);
			g.drawPolygon(
					new int[] { (int) north_west.getX(),
							(int) north_east.getX(), (int) south_east.getX(),
							(int) south_west.getX() }, new int[] {
							(int) north_west.getY(), (int) north_east.getY(),
							(int) south_east.getY(), (int) south_west.getY() },
					4);
			g.setColor(color);
			g.fillPolygon(
					new int[] { (int) north_west.getX(),
							(int) north_east.getX(), (int) south_east.getX(),
							(int) south_west.getX() }, new int[] {
							(int) north_west.getY(), (int) north_east.getY(),
							(int) south_east.getY(), (int) south_west.getY() },
					4);
		}
	}

	@Override
	public void onRepaint(Graphics g) {
		for (RSTile tile : t) {
			drawTile(g, tile, new Color(0, 0, 0, 100));
		}
	}

}
